package com.shield.framework.security.service;

import com.shield.framework.security.auth.AuthUser;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.StringUtil;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * @description: 用户详情
 * @Author Andy
 */
@Slf4j
/*@Service*/
@RequiredArgsConstructor
public class ShieldUserDetailsService implements UserDetailsPasswordService, UserDetailsService {

    @Override
    public UserDetails updatePassword(UserDetails user, String newPassword) {
        return user;
    }

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        if (StringUtil.isBlank(userName)) {
            throw new UsernameNotFoundException("userName is blank!");
        }
        Set<String> dbAuthSet = new HashSet<>();
        Collection<? extends GrantedAuthority> authorities
                = AuthorityUtils.createAuthorityList(dbAuthSet.toArray(new String[0]));
        return new AuthUser(userName, new BCryptPasswordEncoder().encode("123456"), true, true, authorities);
    }
}
